package au.com.iglootech.winerymap.service.webservice;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;
import javax.ws.rs.Consumes;
import javax.ws.rs.FormParam;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;

import org.springframework.stereotype.Service;

import au.com.iglootech.winerymap.entity.WineryInfo;
import au.com.iglootech.winerymap.entity.dto.WineryInfoDTO;
import au.com.iglootech.winerymap.service.WMUserService;
import au.com.iglootech.winerymap.service.WineryInfoService;

import com.google.appengine.api.datastore.KeyFactory;

/**
 * web service implement.
 * 
 * @author zhuhome
 * 
 */
@Service
@Path("/WineryMapWebService")
public class WineryMapWebServiceImpl implements WineryMapWebService {

	/**
	 * 
	 */
	private static final long serialVersionUID = 7049004790363740496L;

	@Resource
	private WineryInfoService wineryInfoService;
	@Resource
	private WMUserService userService;

	@GET
	@Path("/echo/{message}")
	@Override
	public String echo(@PathParam("message") final String echo) {
		return echo;
	}

	@GET
	@Produces({ MediaType.APPLICATION_JSON })
	@Path("/wineryinfo/findAll")
	@Override
	public List<WineryInfoDTO> findAllWinery() {
		List<WineryInfo> infos = wineryInfoService.findAll();
		List<WineryInfoDTO> result = new ArrayList<WineryInfoDTO>();
		if (infos != null) {
			for (WineryInfo info : infos) {
				result.add(new WineryInfoDTO(info));
			}
		}
		return result;
	}

	@GET
	@Produces({ MediaType.APPLICATION_JSON })
	@Path("/wineryinfo/findBykey/{wineryKey}")
	@Override
	public WineryInfoDTO findWineryByKey(
			@PathParam("wineryKey") final String keyValue) {
		WineryInfo info = wineryInfoService.findByKey(KeyFactory
				.stringToKey(keyValue));
		return new WineryInfoDTO(info);
	}

	@Override
	@POST
	@Path("/wineryinfo/addWineryInfo")
	@Consumes(MediaType.APPLICATION_FORM_URLENCODED)
	public String addNewWineryInfo(@FormParam("name") String name,
			@FormParam("region") String region,
			@FormParam("address1") String address1,
			@FormParam("address2") String address2,
			@FormParam("website") String website,
			@FormParam("telephone") String telephone,
			@FormParam("winemaker") String winemaker,
			@FormParam("wineryards") String wineryards,
			@FormParam("facsimilie") String facsimilie,
			@FormParam("description") String description,
			@FormParam("lat") String lat, @FormParam("lng") String lng) {

		return wineryInfoService.addNewWineryInfo(name, region, address1,
				address2, website, telephone, winemaker, wineryards,
				facsimilie, description, lat, lng);
	}

}
